#include <stdlib.h>

#include <TROOT.h>
#include <TApplication.h>
#include <TVirtualX.h>
#include <TVirtualPadEditor.h>
#include <TGResourcePool.h>
#include <TGListBox.h>
#include <TGListTree.h>
#include <TGFSContainer.h>
#include <TGClient.h>
#include <TGFrame.h>
#include <TGIcon.h>
#include <TGLabel.h>
#include <TGButton.h>
#include <TGTextEntry.h>
#include <TGNumberEntry.h>
#include <TGMsgBox.h>
#include <TGMenu.h>
#include <TGCanvas.h>
#include <TGComboBox.h>
#include <TGTab.h>
#include <TGSlider.h>
#include <TGDoubleSlider.h>
#include <TGFileDialog.h>
#include <TGTextEdit.h>
#include <TGShutter.h>
#include <TGProgressBar.h>
#include <TGColorSelect.h>
#include <RQ_OBJECT.h>
#include <TRootEmbeddedCanvas.h>
#include <TCanvas.h>
#include <TH1.h>
#include <TH2.h>
#include <TRandom.h>
#include <TSystem.h>
#include <TSystemDirectory.h>
#include <TEnv.h>
#include <TFile.h>
#include <TKey.h>
#include <TGDockableFrame.h>
#include <TGFontDialog.h>

/*

enum ETestCommandIdentifiers {
   M_FILE_OPEN,
   M_FILE_SAVE,
   M_FILE_SAVEAS,
   M_FILE_PRINT,
   M_FILE_PRINTSETUP,
   M_FILE_EXIT,

   M_TEST_DLG,
   M_TEST_MSGBOX,
   M_TEST_SLIDER,
   M_TEST_SHUTTER,
   M_TEST_DIRLIST,
   M_TEST_FILELIST,
   M_TEST_PROGRESS,
   M_TEST_NUMBERENTRY,
   M_TEST_FONTDIALOG,
   M_TEST_NEWMENU,

   M_VIEW_ENBL_DOCK,
   M_VIEW_ENBL_HIDE,
   M_VIEW_DOCK,
   M_VIEW_UNDOCK,

   M_HELP_CONTENTS,
   M_HELP_SEARCH,
   M_HELP_ABOUT,

   M_CASCADE_1,
   M_CASCADE_2,
   M_CASCADE_3,

   M_NEW_REMOVEMENU,

   VId1,
   HId1,
   VId2,
   HId2,

   VSId1,
   HSId1,
   VSId2,
   HSId2
};


Int_t mb_button_id[9] = { kMBYes, kMBNo, kMBOk, kMBApply,
                          kMBRetry, kMBIgnore, kMBCancel,
                          kMBClose, kMBDismiss };

EMsgBoxIcon mb_icon[4] = { kMBIconStop, kMBIconQuestion,
                           kMBIconExclamation, kMBIconAsterisk };

const char *filetypes[] = { "All files",     "*",
                            "ROOT files",    "*.root",
                            "ROOT macros",   "*.C",
                            "Text files",    "*.[tT][xX][tT]",
                            0,               0 };

struct shutterData_t {
   const char *pixmap_name;
   const char *tip_text;
   Int_t       id;
   TGButton   *button;
};

shutterData_t histo_data[] = {
   { "h1_s.xpm",        "TH1",      1001,  0 },
   { "h2_s.xpm",        "TH2",      1002,  0 },
   { "h3_s.xpm",        "TH3",      1003,  0 },
   { "profile_s.xpm",   "TProfile", 1004,  0 },
   { 0,                 0,          0,     0 }
};

shutterData_t function_data[] = {
   { "f1_s.xpm",        "TF1",      2001,  0 },
   { "f2_s.xpm",        "TF2",      2002,  0 },
   { 0,                 0,          0,     0 }
};

shutterData_t tree_data[] = {
   { "ntuple_s.xpm",    "TNtuple",  3001,  0 },
   { "tree_s.xpm",      "TTree",    3002,  0 },
   { "chain_s.xpm",     "TChain",   3003,  0 },
   { 0,                 0,          0,     0 }
};

*/
/////////////////////////////////////////////////////////////////////////
enum CommandIdentifiers {

  M_FILE_OPEN,
  // M_FILE_NEW,
  M_FILE_SAVE,
  M_FILE_SAVEAS,
  M_FILE_CLESE,
  M_FILE_EXIT,

  M_NEW_RECON,
  M_NEW_ANA
};


const Int_t N_PARTICLE=15;
const Int_t ORDER=4;


class Recon
{
  RQ_OBJECT("Recon")

private:
  TGMainFrame        *fMain;
  TGCompositeFrame   *fv0,*fv1,*fv2 ,*fv3,*fv4,*fv5,*fv6,*fv7,*fv8,*fv9,*fh0,*fh1,*fh2,*fh3,*fh4,*fh5,*fh6,*fh7,*fh8,*fh9,*fh10,*fh11,*fh12,*fh13,*fh14,*fh15,*fh16,*fh17,*fh19,*fh20,*fh21,*fh22,*fh23,*fh24,*fh25,*fh26,*fh27,*fh28,*fh29, *fhParticle[ORDER], *fhRecomasswin_Extrapdg[7], *fhExtrapdg[7];

  TGLayoutHints        *fL1, *fL2, *fL3, *fL4, *fL5, *fL6;
  TGGroupFrame        *fg0,*fg1, *fg2, *fg3, *fg4, *fg5, *fg6, *fg7, *fg8, *fg9;  

  TGButton             *fBLoadin, *fBReset, *fBSubmit, *fBProgress, *fBResult, *fBSave, *fBClose, *fBBrowse, *fBEnter;

  TGRadioButton        *fRExcutemode1,*fRExcutemode2;
  TGCheckButton        *fCReconMethod[6], *fCSavedata[8],*fCRecomasswin, *fCExtrapdg,*fCg1Track, *fCsmTrack, *fCg1DAQ, *fCg1Quality, *fCg1Beam, *fCg1Target, *fCsmRICH; 

  TGComboBox           *fCbDatasource, *fCbExcutenode;

  TGLabel              *fLWorkdir, *fLTaskname,*fLDatasource, *fLExcutenode, *fLExcutemode, *fLRun, *fLDn, *fLDecaymode,  *fLReconmethod, *fLSavedata, *fLBar, *fLParticle[N_PARTICLE], *fLRecomasswin[7], *fLRecomasswinindex[7];
 
  TGTextEntry          *fTTaskname, *fTWorkdir, *fTParticle[N_PARTICLE], *fTExtrapdg[28];
  TGNumberEntry        *fNRun1, *fNRun2, *fNDn, *fNRecomasswin[14];

  
public:
  
  Recon(const TGWindow *p,UInt_t w, UInt_t h);
   virtual ~TestMsgBox();

};


Recon::Recon(const TGWindow *p,UInt_t w, UInt_t h)
{
 
 
  fMain = new TGMainFrame(p, w, h,kHorizontalFrame);
  fMain->SetCleanup(kDeepCleanup);

  {
    fv0=new TGVerticalFrame(fMain,600,30,kLHintsExpandX);//,kFixedWidth);
    {
      fh0=new TGHorizontalFrame(fv0,600,50);//,kFixedHeight);
      {
	fLWorkdir=new TGLabel(fh0, new TGString("WorkDir:"));
	fTWorkdir=new TGTextEntry(fh0, new TGTextBuffer(30));
	fBBrowse=new TGTextButton(fh0, "&Browse", 1);
	
	fh0->AddFrame(fLWorkdir,new TGLayoutHints(kLHintsLeft | kLHintsCenterX,100, 0, 0, 0));
	fh0->AddFrame(fTWorkdir,new TGLayoutHints(kLHintsLeft | kLHintsCenterX,0, 0, 0, 0));
	fh0->AddFrame(fBBrowse,new TGLayoutHints( kLHintsLeft | kLHintsCenterX,0, 100, 0, 0));
   
	fh0->Resize();//10,fTWorkdir->GetDefaultHeight());
      }
      fv0->AddFrame(fh0,new TGLayoutHints(kLHintsTop | kLHintsExpandX,2, 2, 20, 0));
      //////////////////////////////////////////////////////////////////////////

      /////////////////////////////////////////////////////////////////////////
      fh1= new TGHorizontalFrame(fv0,600,30);//,kFixedHeight);
      {
	fBEnter=new TGTextButton(fh1, "&Enter", 2);
	//fBEnter->SetWidth(500);
	
	fh1->AddFrame(fBEnter,new TGLayoutHints(kLHintsCenterX | kLHintsCenterY,3, 5, 20, 0));
	
	//  fBEnter->Resize(300,fBEnter->GetDefaultHeight());
	
	// fh1->Resize();
      }
      fv0->AddFrame(fh1,new TGLayoutHints(kLHintsTop | kLHintsExpandX, 2, 2, 3, 0));
    

      ////////////////////////////////////////////////////////////////////////////

      ///////////////////////////////////////////////////////////////////////////
      fh2=new TGHorizontalFrame(fv0,600,30);//,kFixedWidth);
      {
	fh3=new TGHorizontalFrame(fh2,200,30);//,kFixedWidth);
	
       {
	 fLTaskname=new TGLabel(fh3, new TGString("TaskName:"));
	 fTTaskname=new TGTextEntry(fh3, new TGTextBuffer(8));

	 fh3->AddFrame(fLTaskname,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));

	 fh3->AddFrame(fTTaskname,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));
       }
       fh2->AddFrame(fh3,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));
    
       fv2=new TGVerticalFrame(fh2,200,30);//,kFixedWidth);
       {
	fh4=new TGHorizontalFrame(fv2,200,30);
	
	{
	  fLDatasource=new TGLabel(fh4, new TGString("DataSource:"));
	  fCbDatasource=new TGComboBox(fh4,100);
	  char tt[20];
	  for(int i=0;i<10;i++)
	    {
	      sprintf(tt,"good %d",i++);
	      fCbDatasource->AddEntry(tt,i+1);
	    }
	  fCbDatasource->Resize(190,20);

	  fh4->AddFrame(fLDatasource,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 20));
	  fh4->AddFrame(fCbDatasource,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 20));
	}
	fv2->AddFrame(fh4,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));
	
	fh5=new TGHorizontalFrame(fv2,200,30);
	{
	  fLRun=new TGLabel(fh5, new TGString("run:"));
	  fLBar=new TGLabel(fh5, new TGString("--"));
	  fLDn=new TGLabel(fh5, new TGString("dn:"));

	  fNRun1=new TGNumberEntry(fh5,1001,6,1,(TGNumberFormat::EStyle)0);
	  fNRun2=new TGNumberEntry(fh5,1002,6,1,(TGNumberFormat::EStyle)0);
	  fNDn=new TGNumberEntry(fh5,2,6,1,(TGNumberFormat::EStyle)0);

	  fh5->AddFrame(fLRun,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));
	  fh5->AddFrame(fNRun1,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));
	  fh5->AddFrame(fLBar,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));
	  fh5->AddFrame(fNRun2,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));
	  fh5->AddFrame(fLDn,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));
	  fh5->AddFrame(fNDn,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));

	}

	fv2->AddFrame(fh5,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));

      }
      fh2->AddFrame(fv2,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));

      fv3=new TGVerticalFrame(fh2,200,30);
      {
	fh6=new TGHorizontalFrame(fv3,200,30);
	{
	  fLExcutenode=new TGLabel(fh6, new TGString("ExcuteNode:"));
	  fCbExcutenode=new TGComboBox(fh6,100);

	  fCbExcutenode->Resize(50,20);

	  fh6->AddFrame(fLExcutenode,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));	  
	  fh6->AddFrame(fCbExcutenode,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));
	}
	fv3->AddFrame(fh6,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));
	fg0=new TGGroupFrame(fv3,"ExcuteMode", kVerticalFrame);
	{
	  fRExcutemode1=new TGRadioButton(fg0,"&Foreground",1);
	  fRExcutemode2=new TGRadioButton(fg0,"&Background",2);

	  fg0->AddFrame(fRExcutemode1,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));	
	  fg0->AddFrame(fRExcutemode2,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));	
	}
	fg0->SetTitlePos(TGGroupFrame::kLeft);
	fv3->AddFrame(fg0,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));

      }
      fh2->AddFrame(fv3,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));
    }
      fv0->AddFrame(fh2,new TGLayoutHints(kLHintsTop |kLHintsLeft ,3, 5, 0, 0));

      ////////////////////////////////////////////////////////////////////////////

      //////////////////////////////////////////////////////////////////////////
      fv4=new TGVerticalFrame(fv0,200,30);
      {
	fh7=new TGHorizontalFrame(fv4,200,30);
	{
	  fLDecaymode=new TGLabel(fh7, new TGString("DecayMode:"));
	  
	  fh7->AddFrame(fLDecaymode,new TGLayoutHints(kLHintsCenterX ,3, 5, 0, 0));
	}
	fv4->AddFrame(fh7,new TGLayoutHints(kLHintsTop |kLHintsLeft,3, 5, 0, 0));
	
	
	int iparticle=0;
	
	for(int i=0;i<ORDER;i++)
	  {
	    
	    fhParticle[i]=new TGHorizontalFrame(fv4,(i+1)*160,60,kFixedHeight);//|  kFixedWidth);
	    
	    for(int j=0;j<pow(2,i);j++)
	      {
		
		char id[20];
		sprintf(id,"%d",iparticle);		
		fLParticle[iparticle]=new TGLabel(fhParticle[i], new TGString(id));
		fTParticle[iparticle]=new TGTextEntry(fhParticle[i],new TGTextBuffer(4));
		
		fhParticle[i]->AddFrame(fLParticle[iparticle],new TGLayoutHints(kLHintsLeft ,1, 5, 0, 0));
		fhParticle[i]->AddFrame(fTParticle[iparticle],new TGLayoutHints(/*kLHintsLeft |*/ kLHintsCenterX ,0, 5, 0, 0));		  
		
		
		iparticle++;
	      }
	    fv4->AddFrame(fhParticle[i],new TGLayoutHints(kLHintsTop |kLHintsCenterX,3, 5, 0, 0));
	  }
      }
      fv0->AddFrame(fv4,new TGLayoutHints(kLHintsTop |kLHintsCenterX,3, 5, 0, 0));
	

      ////////////////////////////////////////////////////////////////////////////

      ///////////////////////////////////////////////////////////////////////////
     fh8=new TGHorizontalFrame(fv0,200,30);
     {
       
       fv5=new TGVerticalFrame(fh8,200,30);
       {	
	 fh9=new TGHorizontalFrame(fv5,200,30);
	 {
	   fCRecomasswin=new TGCheckButton(fh9,"Reco&MassWin",1);
	   fCExtrapdg=new TGCheckButton(fh9,"&ExtraPDG",1);
	   
	   fh9->AddFrame(fCRecomasswin, new TGLayoutHints(kLHintsTop |kLHintsLeft,20, 5, 0, 0));        
	   fh9->AddFrame(fCExtrapdg, new TGLayoutHints(kLHintsTop |kLHintsLeft,80, 5, 0, 0));
	   
	 }
	 fv5->AddFrame(fh9, new TGLayoutHints(kLHintsTop,3, 5, 0, 0));
	 
	 int imw=0,iep=0;
	 for(int i=0;i<7;i++)
	   {
	     fhRecomasswin_Extrapdg[i]=new TGHorizontalFrame(fv5,200,40,kFixedHeight);
	     {
	       char ii[20];
	       sprintf(ii,"%d",i);
	       fLRecomasswinindex[i]=new TGLabel(fhRecomasswin_Extrapdg[i], new TGString(ii));
	       fhRecomasswin_Extrapdg[i]->AddFrame(fLRecomasswinindex[i], new TGLayoutHints(kLHintsTop |kLHintsLeft,3, 5, 0, 0));
	       
	       //////////////////////////////////////////////////////////////
	       
	       fNRecomasswin[imw]=new TGNumberEntry(fhRecomasswin_Extrapdg[i],1.106,6,1,(TGNumberFormat::EStyle)3);
	       fhRecomasswin_Extrapdg[i]->AddFrame(fNRecomasswin[imw], new TGLayoutHints(kLHintsTop |kLHintsLeft,3, 5, 0, 0));
	       imw++;
	       
	       fLRecomasswin[i]=new TGLabel(fhRecomasswin_Extrapdg[i], new TGString("--"));
	       fhRecomasswin_Extrapdg[i]->AddFrame(fLRecomasswin[i], new TGLayoutHints(kLHintsTop |kLHintsLeft,3, 5, 0, 0));
	       
	       fNRecomasswin[imw]=new TGNumberEntry(fhRecomasswin_Extrapdg[i],1.116,6,1,(TGNumberFormat::EStyle)3);
	       fhRecomasswin_Extrapdg[i]->AddFrame(fNRecomasswin[imw], new TGLayoutHints(kLHintsTop |kLHintsLeft,3, 5, 0, 0));
	       imw++;
	       
	       ////////////////////////////////////////////////////////
	       
	       for(int j=0;j<4;j++)
		 {
		   fTExtrapdg[iep]=new TGTextEntry(fhRecomasswin_Extrapdg[i],new TGTextBuffer(4));
		   fhRecomasswin_Extrapdg[i]->AddFrame(fTExtrapdg[iep], new TGLayoutHints(kLHintsTop,20, 5, 0, 0));
		   iep++;
		 }
	     }
	     
	     fv5->AddFrame(fhRecomasswin_Extrapdg[i], new TGLayoutHints(kLHintsTop,3, 5, 0, 0));
	   }		
       }
       fh8->AddFrame(fv5, new TGLayoutHints(kLHintsTop,3, 5, 0, 0));

       fg1=new TGGroupFrame(fh8,"SavedParameter", kVerticalFrame);
       {
	 fCg1Track=new TGCheckButton(fg1,"g1Track",1);
	 fCsmTrack=new TGCheckButton(fg1,"smTrack",2);
	 fCg1DAQ=new TGCheckButton(fg1,"g1DAQ",3);
	 fCg1Quality=new TGCheckButton(fg1,"g1Qulity",4);
	 fCg1Beam=new TGCheckButton(fg1,"g1Beam",5);
	 fCg1Target=new TGCheckButton(fg1,"g1Target",6);
	 fCsmRICH=new TGCheckButton(fg1,"smRICH",7);

	 fg1->AddFrame(fCg1Track, new TGLayoutHints(kLHintsTop,3, 5, 5,17));
	 fg1->AddFrame(fCsmTrack, new TGLayoutHints(kLHintsTop,3, 5, 5, 17));
	 fg1->AddFrame(fCg1DAQ, new TGLayoutHints(kLHintsTop,3, 5, 5, 17));
	 fg1->AddFrame(fCg1Quality, new TGLayoutHints(kLHintsTop,3, 5, 5, 17));
	 fg1->AddFrame(fCg1Beam, new TGLayoutHints(kLHintsTop,3, 5, 5, 17));
	 fg1->AddFrame(fCg1Target, new TGLayoutHints(kLHintsTop,3, 5, 5, 17));
	 fg1->AddFrame(fCsmRICH, new TGLayoutHints(kLHintsTop,3, 5, 5, 17));
       }
       fg0->SetTitlePos(TGGroupFrame::kLeft);
       fh8->AddFrame(fg1, new TGLayoutHints(kLHintsTop,3, 5, 0, 0));
    

       
     }
     fv0->AddFrame(fh8, new TGLayoutHints(kLHintsTop,3, 5, 0, 0));
    }

    fMain->AddFrame(fv0,new TGLayoutHints(kLHintsLeft | kLHintsExpandY,2, 2, 3, 0));

    fg2=new TGGroupFrame(fMain,"Action",kVerticalFrame);
    {
      fBReset=new TGTextButton(fg2,"&Reset",1);
      fBLoadin=new TGTextButton(fg2,"&LoadIn...",1);
      fBSubmit=new TGTextButton(fg2,"&Submit...",1);
      fBProgress=new TGTextButton(fg2,"&Progress...",1);
      fBResult=new TGTextButton(fg2,"&Result...",1);
      fBSave=new TGTextButton(fg2,"&Save",1);
      fBSaveas=new TGTextButton(fg2,"Save &as...",1);

      fg2->AddFrame(fBReset, new TGLayoutHints(kLHintsTop,3, 5, 30, 0));
      fg2->AddFrame(fBLoadin, new TGLayoutHints(kLHintsTop,3, 5, 30, 0));
      fg2->AddFrame(fBSubmit, new TGLayoutHints(kLHintsTop,3, 5, 30, 0));
      fg2->AddFrame(fBProgress,new TGLayoutHints(kLHintsTop,3, 5, 30, 0));
      fg2->AddFrame(fBResult, new TGLayoutHints(kLHintsTop,3, 5, 30, 0));
      fg2->AddFrame(fBSave, new TGLayoutHints(kLHintsTop,3, 5, 30, 0));
      fg2->AddFrame(fBSaveas, new TGLayoutHints(kLHintsTop,3, 5, 30, 0));
    }
    fg2->SetTitlePos(TGGroupFrame::kLeft);
    fMain->AddFrame(fg2,new TGLayoutHints(kLHintsLeft | kLHintsExpandY,2, 2, 3, 0));

    
  }

  //////////////////////////////////////////////////////////////////////
   fMain->SetWindowName("testRecon");
   fMain->MapSubwindows();
 
   fMain->Resize();
// fMain->CenterOnParent();
   fMain->MapWindow();
   // gClient->WaitFor(fMain);

}

/////////////////////////////////////////////////////////////////////////
void recon()
{
  new Recon(gClient->GetRoot(), 800, 600);//$$
}

int main(int argc, char **argv)
{
   TApplication theApp("App", &argc, argv);

   if (gROOT->IsBatch()) {
      fprintf(stderr, "%s: cannot run in batch mode\n", argv[0]);
      return 1;
   }

   recon();

   theApp.Run();

   return 0;
}
